﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using LunchCommand.Data;

namespace LunchCommand
{
    public class LunchCommandApplication : HttpApplication
    {
        public LunchCommandContext DatabaseContext { get; private set; }

        public LunchCommandApplication()
        {
            this.BeginRequest += MvcApplication_BeginRequest;
            this.EndRequest += MvcApplication_EndRequest;
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

        }

        void MvcApplication_EndRequest(object sender, EventArgs e)
        {
            this.DatabaseContext.Dispose();
        }

        void MvcApplication_BeginRequest(object sender, EventArgs e)
        {
            this.DatabaseContext = new LunchCommandContext();
        }
    }

    public static class ApplicationExtensions
    {
        public static LunchCommandApplication AsLcAppication(this HttpApplication app)
        {
            return app as LunchCommandApplication;
        }
    }
}
